﻿using System;
using System.Windows.Forms;
using MyMeta;
using Zeus;

namespace MyGenerationSample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private readonly dbRoot myMeta;
        private readonly IZeusInput zeusInput;
        private Button btnOkay;
        private ComboBox cboxDatabases;
        private Label lblDatabases;
        private Label lblTables;
        private ListBox lboxTables;



        public MainForm(dbRoot myMeta, IZeusInput zeusInput)
        {
            this.myMeta = myMeta;
            this.zeusInput = zeusInput;

            InitializeComponent();
        }

        private void MyForm_Load(object sender, EventArgs e)
        {
            cboxDatabases.DataSource = myMeta.Databases;
            cboxDatabases.DisplayMember = "Name";

            if (myMeta.DefaultDatabase != null)
            {
                cboxDatabases.SelectedIndex = cboxDatabases.FindStringExact(myMeta.DefaultDatabase.Name);
            }

            if (myMeta.DefaultDatabase != null)
            {
                lboxTables.DataSource = myMeta.DefaultDatabase.Tables;
                lboxTables.DisplayMember = "Name";
            }
        }

        private void cboxDatabases_SelectionChangeCommitted(object sender, EventArgs e)
        {
            IDatabase database = cboxDatabases.SelectedValue as IDatabase;

            if (database != null)
            {
                lboxTables.DataSource = database.Tables;
                lboxTables.DisplayMember = "Name";
            }
        }

        public void btnOkay_Click(object sender, EventArgs args)
        {
            if ((cboxDatabases.SelectedIndex >= 0) && (lboxTables.SelectedIndex >= 0))
            {
                IDatabase database = cboxDatabases.SelectedValue as IDatabase;
                ITable table = lboxTables.SelectedValue as ITable;

                zeusInput["databaseName"] = database.Name;
                zeusInput["tableName"] = table.Name;

                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                MessageBox.Show("Please choose a Table");
            }
        }
    }
}